package com.example.demo.mycontroller;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.model.CustomResult;
import com.example.demo.model.SysUser;
import com.example.demo.model.TUser;
import com.example.demo.service.ISysUserService;
import com.example.demo.service.ITUserService;
import com.example.demo.service.SysLoginService;
import com.example.demo.utils.JWTUtil;
import com.example.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@RestController
public class LoginController {

    @Autowired
    private ISysUserService userService;

    @PostMapping("/login")
    public CustomResult login(@RequestParam String username, @RequestParam String password) throws UnsupportedEncodingException {
        LambdaQueryWrapper<SysUser>  lqw=new LambdaQueryWrapper<SysUser>();
        lqw.eq(SysUser::getUserName,username);
        lqw.eq(SysUser::getPassword,password);
        SysUser user =userService.getOne(lqw);
        if(user == null){
            return CustomResult.Fail("用户名或密码错误");
        }
        //登录成功则调用JWTUtil类的创建Token方法返回客户端
        String token= JWTUtil.createToken(user);
        return CustomResult.Ok(200,token);
    }

}


